<?php
/**
 * Created by IntelliJ IDEA.
 * User: Zz
 * Date: 2016/5/31
 * Time: 10:42
 */

namespace app\tools;


class Time {

    const STANDARD = 0;
    const YMD      = 1;
    const yMD      = 2;
    /**
     * 友好时间生成 0 标准 1 仅限年月日
     * @param $time
     * @return string
     */
    public static function friendly($time = 0){
        $time == 0 and $time = time();
        return date('Y-m-d',$time);
    }

    public static function short($time){
        $current = time();
        if(($time - $current) > ($current - strtotime(date('Y-m-d', $current)))){
            return date('Y-m-d', $time);
        }else{
            return date('m-d H:i', $time);
        }
    }

    public static function format_micro_time(){
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }

    public static function now(){
        return time();
    }

    /**
     * 输入时间戳，将时间戳更改为时间戳当日0点的时间戳
     * @param $time
     * @return false|int
     */
    public static function dayTimestamp($time){
        return strtotime(date('Y/m/d', $time));
    }

    public static $current = 0;

    public static function exec($step = '',$target = 'time_debug.log'){
        if(Time::$current == 0){
            Time::$current = Time::format_micro_time();
            Time::log([$step, '开始记录'=>Time::$current],$target);
        }else{
            $time = Time::format_micro_time();
            Time::log([$step,'执行时间'=>$time-Time::$current],$target);
            Time::$current = $time;
        }
    }

    public static function log($message,$target = 'time_debug.log'){
        Tool::log($message, $target);
    }
}